<?
function substr_utf8( $str, $start_p , $len_p){
  return preg_replace( '#^(?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,'.$start_p.'}'.'((?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,'.$len_p.'}).*#s','$1' , $str );
}
// $start_p คือตำแหน่งเริ่มต้นตัดข้อความ
// $len_p คือจำนวนตัวอักษรที่ต้องการแสดง
// $data="ข้อความทดสอบ ข้อความทดสอบ ข้อความทดสอบ ข้อความทดสอบข้อความทดสอบ ";

function get_input_date($pefix = ""){
  echo "<select name = '".$pefix."day'>";
    for($i=1; $i<=31; $i++){
      echo "<option value = '".$i."'>".$i."</option>";
    }
  echo "</select>";

  echo "<select name = '".$pefix."month'>";
    for($i=1; $i<=12; $i++) {
      echo "<option value = '".$i."'>".get_month_name($i)."</option>";
    }
  echo "</select>";
  echo "<select name = '".$pefix."year'>";

    for($i=date("Y"); $i>date("Y")-80;$i--) {
      echo "<option value = '".$i."'>".$i."</option>";
    }
  echo "</select>";
}

function thaidate($date) {
  $time = explode(" ",$date);
  $t = explode(":",$time[1]);
  $field = explode("-",$time[0]);

  $field[1] = get_month_name($field[1]);

  if($field[2] == "01") $field[2] = "1";
  if($field[2] == "02") $field[2] = "2";
  if($field[2] == "03") $field[2] = "3";
  if($field[2] == "04") $field[2] = "4";
  if($field[2] == "05") $field[2] = "5";
  if($field[2] == "06") $field[2] = "6";
  if($field[2] == "07") $field[2] = "7";
  if($field[2] == "08") $field[2] = "8";
  if($field[2] == "09") $field[2] = "9";
  $field[0] = $field[0];

  return "$field[2] $field[1] $field[0] เวลา $t[0]:$t[1]";
}

function get_path($folder){

  $y = date("Y");
  $m = date("m");

  if( !file_exists($folder) ){
    mkdir($folder, 0777);
  }

  if( !file_exists($folder."/".$y) ){
    mkdir($folder."/".$y, 0777);
  }

  if( !file_exists($folder."/".$y."/".$m) ){
    mkdir($folder."/".$y."/".$m, 0777);
  }

  return $folder."/".$y."/".$m;
}

function get_url_path($path){
  $new = '';
  $p = explode("/",$path);
  for($i = 0; $i < count($p); $i++){
    if($p[$i] != ".." && $p[$i] != "."){
      $new .= $p[$i]."/";
    }
  }

  return substr($new, 0, -1);
}

function get_file_name($id, $file){

  $image_info = getimagesize($file);
  if( $image_info[2] == IMAGETYPE_JPEG ) {
     $ext = ".jpg";
  } elseif( $image_info[2] == IMAGETYPE_GIF ) {
     $ext = ".gif";
  } elseif( $image_info[2] == IMAGETYPE_PNG ) {
     $ext = ".png";
  }

  if($id == ""){
    $id = 0;
  }

  if(!file_exists($file)){
    $newfile = $id."-".time()."-".remote_file_size($file).$ext;
  }else{
    $newfile = $id."-".time()."-".filesize($file).$ext;
  }

  return $newfile;
}

function is_url($url){

  if(strrchr($url,"http://")){
    return true;
  }

  if(strrchr($url,"www.")){
    return true;
  }

  if(strrchr($url,".com")){
    return true;
  }

  return false;
}

function url_exists($url) {
    // Version 4.x supported
    $handle   = curl_init($url);
    if (false === $handle)
    {
        return false;
    }
    curl_setopt($handle, CURLOPT_HEADER, false);
    curl_setopt($handle, CURLOPT_FAILONERROR, true);  // this works
    curl_setopt($handle, CURLOPT_HTTPHEADER, Array("User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.15) Gecko/20080623 Firefox/2.0.0.15") ); // request as if Firefox   
    curl_setopt($handle, CURLOPT_NOBODY, true);
    curl_setopt($handle, CURLOPT_RETURNTRANSFER, false);
    $connectable = curl_exec($handle);
    curl_close($handle);  
    return $connectable;
}

function close(){
  echo "<html><body onLoad=\"javascript:self.close();\">";
  echo "</body></html>";
}

function close_and_refresh(){
   echo "<html><body onLoad=\"javascript:self.close();window.opener.location.reload();\">";
   echo "</body></html>";
}

function redirect($url,$wait=0) {
  echo "<html><head>";
  echo "<meta http-equiv='refresh' content='$wait; URL=$url'>";
  echo "</head></html>";
  exit;
}


?>